<template>
    <div>
      <h1>BillPage</h1>
      <div class="bill-container">
        <h1>账单列表</h1>
        <el-select v-model="selectedStatus" placeholder="请选择支付状态" @change="filterBills">
          <el-option label="全部" value=""></el-option>
          <el-option label="待支付" value="PENDING"></el-option>
          <el-option label="成功" value="SUCCESS"></el-option>
          <el-option label="失败" value="FAILED"></el-option>
          <el-option label="已取消" value="CANCELLED"></el-option>
        </el-select>
  
        <el-table :data="currentPageBills" style="width: 100%">
          <el-table-column prop="billId" label="账单ID"></el-table-column>
          <el-table-column prop="paymentPartyName" label="支付方"></el-table-column>
          <el-table-column prop="initiatorName" label="发起人"></el-table-column>
          <el-table-column prop="amount" label="金额"></el-table-column>
          <el-table-column prop="timestamp" label="时间"></el-table-column>
          <el-table-column prop="paymentStatus" label="支付状态"></el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <el-button v-if="scope.row.paymentStatus === 'PENDING'" type="primary" @click="payBill(scope.row)">支付</el-button>
            </template>
          </el-table-column>
        </el-table>
  
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalBills"
        ></el-pagination>
      </div>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  import { ElMessage } from 'element-plus';
  
  export default {
    name: 'BillPage',
    data() {
      return {
        bills: [],
        filteredBills: [],
        currentPage: 1,
        pageSize: 10,
        totalBills: 0,
        selectedStatus: ''
      };
    },
    created() {
      this.fetchBills();
    },
    computed: {
      currentPageBills() {
        const startIndex = (this.currentPage - 1) * this.pageSize;
        return this.filteredBills.slice(startIndex, startIndex + this.pageSize);
      }
    },
    methods: {
      fetchBills() {
        axios.get('http://localhost:8085/api/bill', {
          params: {
            userId: 1 // 假设当前用户的ID为1
          }
        })
        .then(response => {
          if (response.data.code === 1) {
            this.bills = response.data.data;
            this.filteredBills = this.bills;
            this.totalBills = this.filteredBills.length;
          } else {
            ElMessage.error('获取账单数据失败');
          }
        })
        .catch(() => {
          ElMessage.error('获取账单数据失败');
        });
      },
      filterBills() {
        if (this.selectedStatus) {
          this.filteredBills = this.bills.filter(bill => bill.paymentStatus === this.selectedStatus);
        } else {
          this.filteredBills = this.bills;
        }
        this.totalBills = this.filteredBills.length;
        this.currentPage = 1; // 重置到第一页
      },
      handleSizeChange(size) {
        this.pageSize = size;
      },
      handleCurrentChange(page) {
        this.currentPage = page;
      },
      payBill(bill) {
        // 调用后端接口生成支付宝支付链接
        axios.post('http://localhost:8085/api/pay', {
          billId: bill.billId,
          amount: bill.amount
        })
        .then(response => {
          if (response.data.code === 1) {
            // 跳转到支付宝支付页面
            window.location.href = response.data.paymentUrl;
          } else {
            ElMessage.error('生成支付链接失败');
          }
        })
        .catch(() => {
          ElMessage.error('生成支付链接失败');
        });
      }
    }
  };
  </script>
  
  <style scoped>
  .bill-container {
    padding: 20px;
  }
  </style>
  